
//axisのオブジェクト
function Zzaxis(aid, name){

    // aid
    this.aid = aid;

    // name
    this.name = name;

    // cell
    this.cells = new Array;

    // theta
    this.theta = null;

    // 目標とするtheta
    this.to_theta = null;

    // 標示するかしないか
    this.display = false;

    // １フレームあたりに移動する移動量
    this.dtheta = null;

    //軸の色
    this.color = "";

    //軸の色（グラデーション開始）
    this.sicklycolor = "";

    //選択中の軸か
    this.focus = false;

    // cellを追加
    this.addcell = function(cell){
        this.cells.push(cell);
    };

    //固定する軸か
    this.hold = false;

    //軸の次の位置をセット
    //ToDo:どこかでNaNになる。FFで落ちる原因。
    this.setToTheta = function(focused_axis_no, clicked_axis_no){
        var target_axis_no = this.axis_no;
        var move_value = clicked_axis_no - focused_axis_no;
        var difference_focus_and_me = this.axis_no - focused_axis_no;
        var alpha = Math.PI / 2 - Math.atan(settings.height / settings.width);
        var beta = Math.atan(settings.height / settings.width);
        var angle_array = [beta, beta, alpha, alpha];
        var delta_theta = sumDeltaTheta(difference_focus_and_me, move_value, angle_array);
        //console.log(delta_theta);
        this.to_theta = this.theta + delta_theta;
        this.setDTheta(delta_theta, move_value);
    };

    this.setDTheta = function(delta_theta, move_value){
        this.dtheta = delta_theta / (15 * Math.abs(move_value));
    };


    this.update = function(){
	
        //角度がPI/10以下もしくはPI*9/10以上になったら表示しない
        if (this.theta > Math.PI * 9 / 10 || this.theta < Math.PI / 10) {
            this.display = false;
        }
        else {
            if (axis_moving) {
                this.display = true;
            }
        }
	
	//ダミー軸は表示しない
	if (this.aid == 99){
	    //console.log("dummy");
	    this.display = false;
	}
	
        //目標のthetaになるまで移動量を現在のthetaに足してゆく
        if (this.dtheta > 0) {
            //反時計回り
            if ((this.theta < this.to_theta)) {
                this.theta += this.dtheta;
                axis_moving = true;
            //debug();
            }
            else {
                this.theta = this.to_theta;
                axis_moving = false;
            }
        }
        else {
            //時計回り
            if ((this.theta > this.to_theta)) {
                this.theta += this.dtheta;
                axis_moving = true;
            }
            else {
                this.theta = this.to_theta;
                axis_moving = false;
            }
        }

        this.draw();
    };

    // draw
    this.draw = function(){
        if (this.display) {
            ctx.strokeStyle = this.color;
            if (this.focus) {
                ctx.lineWidth = 12;
            }
            else {
                ctx.lineWidth = 12;
            }
            ctx.beginPath();
            ctx.moveTo(x(settings.height/2/Math.tan(this.theta)), y(settings.height/2));
            var grad = ctx.createLinearGradient(x(settings.height/2/Math.tan(this.theta)), y(settings.height/2), x((-settings.height/2+20)/Math.tan(this.theta)), y(-settings.height/2+20));
            grad.addColorStop(0,this.sicklycolor);
            grad.addColorStop(1,this.color);
            ctx.strokeStyle = grad;
            ctx.lineTo(x((-settings.height/2+20)/Math.tan(this.theta)), y(-settings.height/2+20));
            ctx.closePath();
            ctx.stroke();

            //矢印部分の描画
            ctx.beginPath();
            ctx.lineWidth = 10;
            ctx.fillStyle = this.color;
            ctx.moveTo(x((-settings.height/2+5)/Math.tan(this.theta)), y(-settings.height/2+5));
            ctx.lineTo(x((-settings.height/2+5)/Math.tan(this.theta) + 40*Math.cos(this.theta - Math.PI/6)), y(-settings.height/2+5 + 40*Math.sin(this.theta - Math.PI/6)));
            ctx.lineTo(x((-settings.height/2+5)/Math.tan(this.theta) + 40*Math.cos(this.theta + Math.PI/6)), y(-settings.height/2+5 + 40*Math.sin(this.theta + Math.PI/6)));
            ctx.closePath();
            ctx.fill();

            //オブジェクトが持つcellを描画
            var local_theta = this.theta;
            var local_to_theta = this.to_theta;
            var local_focus = this.focus;
            if (this.cells) {
                jQuery.each(this.cells, function(i, val){
                    if (local_theta != local_to_theta) {
                        //中心以外のセルの座標を変更
                        if (i != 0) {
                            var cell_x = 100 * Math.pow(1.1, local_theta + Math.PI * (i - 1)) * Math.cos(local_theta + Math.PI * (i - 1));
                            var cell_y = 100 * Math.pow(1.1, local_theta + Math.PI * (i - 1)) * Math.sin(local_theta + Math.PI * (i - 1));
                            val.x = x(cell_x);
                            val.y = y(cell_y);
                        }
                        else {
                            //もしfocusの軸なら中心セルの標示フラグを変更
                            //if (local_focus) {
                                this.display = true;
                            //}
                        }
                    }
                    val.update();
                });
            }

        }
    };
    this.dist_from_focus = function(){
        //focusの軸からの距離
        var focus_axis_no;
        jQuery.each(axises, function(i, axis){
            if(axis.focus){
                focus_axis_no = axis.axis_no;
            }
        });
        return this.axis_no - focus_axis_no;
    };
}